package io.gitee.yxsnake.embark.web.core.configuration;

import io.gitee.yxsnake.embark.web.core.concurrent.EmbarkThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author snake
 * @description Spring 线程池配置
 * @since 2024/10/17 1:13
 */
@Configuration
public class ThreadPoolConfiguration {
    @Bean
    public ThreadPoolTaskExecutor initCommonThreadPool() {
        ThreadPoolTaskExecutor threadPool = new EmbarkThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(2);//核心线程数
        threadPool.setMaxPoolSize(4);//最大线程数
        threadPool.setKeepAliveSeconds(60);//线程存活时间
        threadPool.setQueueCapacity(10000);
        threadPool.setAllowCoreThreadTimeOut(false);
        threadPool.setThreadNamePrefix("common-thread-");//线程池前缀
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//拒绝策略， 调用者执行
        return threadPool;
    }
}
